package net.suncaper.psychological.web.controller;

import net.suncaper.psychological.common.framework.simplemvc.RequestMapping;
import net.suncaper.psychological.common.utils.Result;
import net.suncaper.psychological.common.utils.MyDbUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 测试控制器
 */
@RequestMapping("/api/test")
public class TestController {

    /**
     * 测试查询预约ID为26的数据
     */
    @RequestMapping("/appointment-26")
    public void testAppointment26(HttpServletRequest request, HttpServletResponse response) {
        try {
            String sql = "SELECT " +
                        "a.id as appointment_id, " +
                        "a.student_id, " +
                        "a.duty_id, " +
                        "a.type, " +
                        "a.status, " +
                        "u.username as student_username, " +
                        "u.name as student_name, " +
                        "ds.user_id as consultant_id, " +
                        "c.name as consultant_name " +
                        "FROM appointment a " +
                        "LEFT JOIN user u ON a.student_id = u.id " +
                        "LEFT JOIN duty_schedule ds ON a.duty_id = ds.id " +
                        "LEFT JOIN user c ON ds.user_id = c.id " +
                        "WHERE a.id = 26";
            
            System.out.println("测试查询SQL: " + sql);
            List<Map<String, Object>> result = MyDbUtil.executeQueryMap(sql);
            System.out.println("查询结果: " + result);
            
            Result.success(result, "查询成功").writeTo(response);
        } catch (Exception e) {
            e.printStackTrace();
            Result.error("查询失败: " + e.getMessage()).writeTo(response);
        }
    }

    /**
     * 测试查询咨询师ID为10的所有预约
     */
    @RequestMapping("/consultant-10")
    public void testConsultant10(HttpServletRequest request, HttpServletResponse response) {
        try {
            String sql = "SELECT " +
                        "a.id as appointment_id, " +
                        "a.student_id, " +
                        "a.duty_id, " +
                        "a.type, " +
                        "a.status, " +
                        "u.username as student_username, " +
                        "u.name as student_name, " +
                        "ds.user_id as consultant_id, " +
                        "c.name as consultant_name " +
                        "FROM appointment a " +
                        "INNER JOIN duty_schedule ds ON a.duty_id = ds.id " +
                        "INNER JOIN user u ON a.student_id = u.id " +
                        "INNER JOIN user c ON ds.user_id = c.id " +
                        "WHERE ds.user_id = 10 " +
                        "AND a.type = 'CONSULT'";
            
            System.out.println("咨询师10查询SQL: " + sql);
            List<Map<String, Object>> result = MyDbUtil.executeQueryMap(sql);
            System.out.println("咨询师10查询结果: " + result);
            
            Result.success(result, "查询成功").writeTo(response);
        } catch (Exception e) {
            e.printStackTrace();
            Result.error("查询失败: " + e.getMessage()).writeTo(response);
        }
    }

    /**
     * 测试查询所有咨询预约
     */
    @RequestMapping("/all-consult")
    public void testAllConsult(HttpServletRequest request, HttpServletResponse response) {
        try {
            String sql = "SELECT " +
                        "a.id as appointment_id, " +
                        "a.student_id, " +
                        "a.duty_id, " +
                        "a.type, " +
                        "a.status, " +
                        "u.username as student_username, " +
                        "u.name as student_name, " +
                        "ds.user_id as consultant_id, " +
                        "c.name as consultant_name " +
                        "FROM appointment a " +
                        "LEFT JOIN user u ON a.student_id = u.id " +
                        "LEFT JOIN duty_schedule ds ON a.duty_id = ds.id " +
                        "LEFT JOIN user c ON ds.user_id = c.id " +
                        "WHERE a.type = 'CONSULT' " +
                        "ORDER BY a.id";
            
            System.out.println("所有咨询预约查询SQL: " + sql);
            List<Map<String, Object>> result = MyDbUtil.executeQueryMap(sql);
            System.out.println("所有咨询预约查询结果: " + result);
            
            Result.success(result, "查询成功").writeTo(response);
        } catch (Exception e) {
            e.printStackTrace();
            Result.error("查询失败: " + e.getMessage()).writeTo(response);
        }
    }
} 